import {
    get,
    post
} from '../../../utils/request'
import {
    APIS
} from '../../../utils/api'
Component({
    properties: {
        isNoStorage: {
            type: Boolean,
            default: false
        }
    },
    data: {
        currentIndex: 1,
        shopArray: [],
        shopIds: [],
        shopList: [],
    },
    lifetimes: {
        attached() {
            this.getShopData();
        }
    },
    methods: {
        bindChangeShop(e) {
            const index = e.detail.value;
            this.setData({
                currentIndex: index,
            });
            if (this.data.isNoStorage) {
                this.triggerEvent('getShopIndex', this.data.shopIds[index]);
                this.triggerEvent('getShopName', this.data.shopArray[index]);
            }
            else {
                wx.setStorageSync('currentShop', this.data.shopIds[index]);
                wx.setStorageSync('shopInfo', this.data.shopList[index]);
                this.triggerEvent('getShopInfo', this.data.shopList[index]);
            }
        },
        getShopData() {
            get(APIS.shop).then(res => {
                const list = res.data;
                if (!list.length) {
                    return []
                }
                const sid = this.data.isNoStorage ? list[0].id : (wx.getStorageSync('currentShop') || list[0].id);
                const shopArray = list.map(x => x.name);
                const shopIds = list.map(x => x.id);
                this.setData({
                    shopArray,
                    shopIds,
                    shopList: list,
                    currentIndex: shopIds.findIndex(x => x === sid)
                });
                if (!this.data.isNoStorage) {
                    wx.setStorageSync('currentShop', sid);
                }
                else {
                    this.triggerEvent('getShopName', shopArray[0]);
                }
            })
        },
    },
})